﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using System.Drawing.Printing;

namespace FlowCard
{
    public partial class frmPreview : Form
    {
        ReportDocument rd;
        DataTable dt;
        string strCurrentTemplate;
        string strCardType;
        public bool Printed = false;
        string strOperator = string.Empty;
        string strGDNo = string.Empty;

        public frmPreview(string strTmpOperator,string strTmpGDNo,DataTable tmpdt,string strTmpReportFileName,string strTmpCardType)
        {
            InitializeComponent();
            iniPrintersComboBox();
            strOperator = strTmpOperator;
            strGDNo = strTmpGDNo;
            try
            {
                rd = new ReportDocument();
                rd.AfterFormatPage += rd_AfterFormatPage;
                rd.Load(strTmpReportFileName);
                strCurrentTemplate = strTmpReportFileName.Replace(Application.StartupPath + "\\Cards\\", string.Empty);
                txtCurrentTemplate.Text = strCurrentTemplate;
                strCardType = strTmpCardType;
                dt = tmpdt;
                rd.SetDataSource(dt);
                crystalReportViewer1.ReportSource = rd;
                rd.PrintOptions.PrinterDuplex = CrystalDecisions.Shared.PrinterDuplex.Vertical ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            iniComboBox();

        }

        private void rd_AfterFormatPage(FormatPageEventArgs e)
        {
            rd.Dispose();
        }

        public static bool ShowForm(string strTmpOperator,string strTmpGDNo, DataTable tmpdt, string strTmpReportFileName, string strTmpCardType)
        {
            frmPreview newFrmPreview = new frmPreview(strTmpOperator, strTmpGDNo, tmpdt, strTmpReportFileName, strTmpCardType);
            newFrmPreview.ShowDialog();
            return newFrmPreview.Printed;
        }

        private void btnChangeTemplate_Click(object sender, EventArgs e)
        {
            if (!cmbTemplates.Text.Equals(string.Empty))
            {
                rd.Load(Application.StartupPath + "\\Cards\\" + cmbTemplates.Text+".rpt");
                rd.SetDataSource(dt);
                crystalReportViewer1.ReportSource = rd;
            }
        }

        private void iniComboBox()
        {
            cmbTemplates.Items.Clear();
            switch (strCardType)
            {
                case "A":
                    cmbTemplates.Items.Add("MLCC生产流转卡A卡");
                    cmbTemplates.Items.Add("国军标片式MLCC生产流转卡A卡");
                    cmbTemplates.Items.Add("国军标引线MLCC生产流转卡A卡");
                    cmbTemplates.Items.Add("宇航级片式MLCC生产流转卡A卡");
                    cmbTemplates.Items.Add("宇航级引线MLCC生产流转卡A卡");
                    cmbTemplates.Items.Add("TJ片式MLCC生产流转卡A卡");
                    cmbTemplates.Items.Add("TJ引线MLCC生产流转卡A卡");
                    break;
                case "B":
                    cmbTemplates.Items.Add("片式MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("引线MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("国军标片式MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("国军标引线MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("宇航级片式MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("宇航级引线MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("TJ片式MLCC生产流转卡B卡");
                    cmbTemplates.Items.Add("TJ引线MLCC生产流转卡B卡");
                    break;
                case "C":
                    cmbTemplates.Items.Add("引线MLCC生产流转卡C卡");
                    break;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (cmbPrinters.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("未查询到有效的打印机，请联系管理员！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            SQLTools sqt = new SQLTools();
            if (sqt.RecordExist("select ID from CardPrintLog where GDNo='" + strGDNo + "' and Failure=0"))
            {
                MessageBox.Show("还有未失效的打印记录，请先将所有未失效的记录处理后再打印此卡！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                frmLogViewer newFrmLogViewer = new frmLogViewer(strOperator,strGDNo);
                newFrmLogViewer.Show();
                return;
            }
            LogHelper.WriteLog(strOperator, strGDNo, dt.Rows[0]);
            rd.PrintOptions.PrinterName = cmbPrinters.Text;
            rd.PrintOptions.PrinterDuplex = CrystalDecisions.Shared.PrinterDuplex.Vertical ;
            rd.PrintToPrinter(1, false, 0, 0);
            Printed = true;
        }

        private void iniPrintersComboBox()
        {
            try
            {
                foreach (string Printer in PrinterSettings.InstalledPrinters)
                {
                    cmbPrinters.Items.Add(Printer);
                }
                if (cmbPrinters.Items.Count > 0)
                {
                    cmbPrinters.SelectedIndex = 0;
                }
                if (!Properties.Settings.Default.DefaultPrinter.Trim().Equals(string.Empty))
                {
                    cmbPrinters.Text = Properties.Settings.Default.DefaultPrinter;
                }
            }
            catch
            {
                MessageBox.Show("获取打印机名错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        private void btnSetDefualt_Click(object sender, EventArgs e)
        {
            if (!cmbPrinters.Text.Trim().Equals(string.Empty))
            {
                Properties.Settings.Default.DefaultPrinter = cmbPrinters.Text;
            }
        }
    }
}
